import React from 'react';
import styles from './style.css';
import PropTypes from 'prop-types';
import {FORM_OPTIONS} from '../constants';
import TextArea from 'Shared/components/TextArea';
import WrappedSelect from '../common/WrappedSelect';
import {injectIntl, language, formatMessage} from './intl';
import {Form, Row, Col, Spin, Collapse, Button} from 'antd';
import MultipleSelect from '../ClaimReportForm/MultipleSelect';
import DateRangePicker from 'Shared/components/DateRangePicker';
import {marketSelect} from '../Enum';
class QueryPanel extends React.PureComponent {
    state = {
        loading: false,
        marketSelect: 1
    }
    // eslint-disable-next-line react/sort-comp
    onResetClick = () => {
        this.setState({marketSelect: 1});
        this.props.onResetClick();
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    onClickStopPropagation = e => {
        e.stopPropagation();
    }
    handleMarketSelect = value => {
        this.setState({marketSelect: value});
        this.props.onConditionsChange('regions', []);
        this.props.onConditionsChange('dealerCodes', []);
    }
    render() {
        const {...rest} = this.state;
        const {conditions, loading, categories} = this.props;
        return (
            <div className={styles.collapse}>
                <Spin spinning={loading}>
                    <Collapse>
                        <Collapse.Panel key="1" disabled={true} header={<Row>
                            <Col span={2} className={styles.titleName}>
                                {formatMessage({
                                    id: 'queryPanel.form.marketInfo',
                                    defaultMessage: '市场信息'
                                })}
                            </Col>
                            <Col span={22}>
                                <Row>
                                    <Form className="form-standard">
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.marketSelect',
                                                    defaultMessage: '市场选择'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><WrappedSelect
                                                    name="marketSelect"
                                                    value={rest.marketSelect}
                                                    options={marketSelect.toList(language)}
                                                    onChange={this.handleMarketSelect} /></div>
                                            </Form.Item>
                                        </Col>
                                        {rest.marketSelect === 2 &&
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.marketDepartmentId',
                                                    defaultMessage: '区域'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><MultipleSelect
                                                    name="regions"
                                                    placeholder={formatMessage({
                                                        id: 'queryPanel.form.region.placeholder',
                                                        defaultMessage: '最多选择1000个大区名称，以回车隔开'
                                                    })}
                                                    dataOptions={categories}
                                                    value={conditions.regions}
                                                    onBlur={this.handleFilterChange} /></div>
                                            </Form.Item>
                                        </Col>}
                                        {rest.marketSelect === 3 &&
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.dealerId',
                                                    defaultMessage: '总代'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><TextArea
                                                    name="dealerCodes"
                                                    placeholder={formatMessage({
                                                        id: 'queryPanel.form.dealerCode.placeholder',
                                                        defaultMessage: '最多输入1000个总代名称，以回车隔开'
                                                    })}
                                                    value={conditions.dealerCodes}
                                                    onBlur={this.handleFilterChange} /></div>
                                            </Form.Item>
                                        </Col>}
                                    </Form>
                                </Row>
                            </Col>
                        </Row>} />
                        <Collapse.Panel key="2" disabled={true} header={<Row>
                            <Col span={2} className={styles.titleName}>
                                {formatMessage({
                                    id: 'queryPanel.form.indexInfo',
                                    defaultMessage: '基础信息'
                                })}
                            </Col>
                            <Col span={22}>
                                <Row>
                                    <Form className="form-standard">
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.submitTime',
                                                    defaultMessage: '提交时间'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}>
                                                    <DateRangePicker name="submitTime" value={conditions.submitTime}
                                                        onChange={this.handleFilterChange} /></div>
                                            </Form.Item>
                                        </Col>
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.approveTime',
                                                    defaultMessage: '审核时间'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}>
                                                    <DateRangePicker name="approveTime" value={conditions.approveTime}
                                                        onChange={this.handleFilterChange} /></div>
                                            </Form.Item>
                                        </Col>
                                    </Form>
                                </Row>
                            </Col>
                        </Row>}>
                        </Collapse.Panel>
                    </Collapse>
                    <Form className={styles.fromtop}>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={this.props.querySearch} loading={loading}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.query',
                                        defaultMessage: '查询'
                                    })}
                                </Button>
                                <Button key="clear" onClick={this.onResetClick}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.reset',
                                        defaultMessage: '重置'
                                    })}
                                </Button>
                            </Col>
                        </Row>
                    </Form>
                </Spin>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    categories: PropTypes.array
};

import {connect} from 'react-redux';
import * as actions from './actions';
import {createSelector} from 'reselect';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getCategoryData = createSelector(
    state => state.getIn(['page', 'domainData', 'category']),
    init => {
        const initData = init.toJS();
        const categories = initData.data ? initData.data.map(item => ({
            code: item.code,
            name: item.name
        })) : [];
        return categories;
    }
);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    categories: getCategoryData(state),
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
});

const mapDispatchToProps = dispatch => ({
    querySearch: () => dispatch(actions.querySearch()),
    onResetClick: () => dispatch(actions.resetQueryPanel()),
    onConditionsChange: (name, value) => dispatch(actions.saveQueryCondition(name, value)),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
